jQuery(function($) {
    var doc = $(document),
        doc_ = document;
    
    function changeSubTag(elm, box, lis) { 
        var index = elm.index();
            
        $.each(box, function(i) {
            lis.eq(i).removeClass("selected");
            box.eq(i).hide();
        });
        
        lis.eq(index).addClass("selected");
        box.eq(index).show();
    }
    
    //切换‘我的账号’
    doc.on("click", ".sub-list-1 li", function() {
        changeSubTag($(this), $(".sub-acc-box"), $("section .sub-list-1 li"));
    });
    
    doc.on("click", ".mob-1 li", function() {
        changeSubTag($(this), $(".sub-acc-box"), $(".mob-1 li"));
        $(".sidebar.move").removeClass("move");
    });
    
    //切换‘约谈关注’
    doc.on("click", ".interview .sub-list-3 li", function() {
        changeSubTag($(this), $(".cre-pro"), $(".interview .sub-list-3 li"));
    });
    doc.on("click", ".mob-2 li", function() {
        changeSubTag($(this), $(".cre-pro"), $(".mob-2 li"));
        $(".sidebar.move").removeClass("move");
    });
    
    //切换‘投资管理’
    doc.on("click", ".inv-run .sub-list-5 li", function() {
        changeSubTag($(this), $(".inv-list"), $(".inv-run .sub-list-5 li"));
    });
    doc.on("click", ".inv-terr .sub-list-6 li", function() {
        changeSubTag($(this), $(".finan_box"), $(".inv-terr .sub-list-6 li"));
    });
    
    doc.on("click", ".mob-3 li", function() {
        changeSubTag($(this), $(".inv-list"), $(".mob-3 li"));
        $(".sidebar.move").removeClass("move");
    });
    doc.on("click", ".mob-4 li", function() {
        changeSubTag($(this), $(".finan_box"), $(".mob-4 li"));
        $(".sidebar.move").removeClass("move");
    });
    
    //切换‘融资平台’
    
    //切换表单数据
    function showTabs(elm, index) {
        var lis = $("nav .pos"),
            sub_nav = $(".mob-sub-list"),
            sections = $("section");
        
        $.each(lis, function(i) {
            lis.eq(i).removeClass("selected");
            sections.eq(i).hide();
        });
        sections.eq(index).show();
        lis.eq(index).addClass("selected");
        
        $.each(sub_nav, function(i, item) {
            sub_nav.eq(i).hide();
        });
        
        switch(index) {
            case 0:
                $(".mob-1").show();
                break;
            case 2:
                $(".mob-2").show();
                break;
            case 4:
                $(".mob-3").show();
                break;
            case 5:
                $(".mob-4").show();
                break;
        }
    }
    
    //切换主导航
    doc.on("click", ".sidebar nav .pos", function() {
        var elm = $(this),
            index = elm.index();
    
        showTabs(elm, index);
    });
    
    //手机端导航切换
    doc.on("click", ".mob-fixed-bar button", function() {
        var elm = $(".sidebar");
        elm.toggleClass("move");
    });
    
    
    
    /**********************表单验证*******************/
    var account = {
        realName: false,
        email: false,
        mobile: false,
        desc: false,
        role: false,
        education: false,
        address: false
    };
    
        //校验手机号
    function checkMobile(elm, err) {
        var tel = /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/g;

        if (!tel.test(elm.val())) {
            err.show();
            sendErrorMessage(err, "请输入正确的手机号码！");
            elm.css({
                border: "1px solid #f3a3a3"
            });
            account.phone = false;
            return;
        }
        account.phone = true;
    }
    
        //校验邮箱
    function checkEmail(elm, err) {
        var reg = /([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})/;
        
        if (!reg.test(elm.val())) {
            err.show();
            sendErrorMessage(err, "请输入正确的邮箱！");
            account.email = false;
            return;
        }
        account.email = true;
    }
    
        //显示错误信息
    function sendErrorMessage(node, err) {
        node.html(err).show();
    }
        
        //判断是否为空
    function empty(val) {
        if (val === "") {
            return true;
        }
        return false;
    }
    
    /*账号信息 start*/
        //真实姓名
    function checkRealName(elm, err) {
        var val = elm.val();
        
        //检测是否为空
        if (empty(val)) {
            sendErrorMessage(err, "真实姓名不能为空！");
            account.realName = false;
            return;
        }
        
        //检测字符长度
        if (val.length > 4) {
            sendErrorMessage(err, "真实姓名请保持在4个字符以内！");
            account.realName = false;
            return;
        }
        account.realName = true;
    }
    
    doc.on("blur", ".real_name_box .real_name", function() {
        checkRealName($(this), $(".real_name_box .real_name_err"));
    });
    
    doc.on("focus", ".real_name_box .real_name", function() {
        $(".real_name_box .real_name_err").html("").hide();
        
    });
    
        //检测邮箱
    doc.on("blur", ".email_box .email", function() {
        checkEmail($(this), $(".email_box .email_err"));
    });
    
    doc.on("focus", ".email_box .email", function() {
        $(".email_box .email_err").html("").hide();
    });
    
        //检测手机号码
    doc.on("blur", ".mobile_box .mobile", function() {
        checkMobile($(this), $(".mobile_box .mobile_err"));
    });
    
    doc.on("focus", ".mobile_box .mobile", function() {
        $(".mobile_box .mobile_err").html("").hide();
    });
    
        //检测简介
    function checkDesc(elm, err) {
        var val = elm.val();
        if (empty(val)) {
            sendErrorMessage(err, "简介不能为空！");
            account.desc = false;
            return;
        }
        
        if (val.length > 30) {
            sendErrorMessage(err, "简介内容请保持在30个字符以内！");
            account.desc = false;
            return;
        }
        account.desc = true;
    }
    
    doc.on("blur", ".desc_box textarea",  function() {
        checkDesc($(this), $(".desc_box .desc_err"));
    });
    
    doc.on("focus", ".desc_box textarea",  function() {
        $(".desc_box .desc_err").html("").hide();
    });
    
        //检测全部
    doc.on("click", ".sub-acc-box .submit a", function(event) {
        event.preventDefault();
        checkRealName($(".real_name"), $(".real_name_box .real_name_err"));
        checkEmail($(".email"), $(".email_box .email_err"));
        checkMobile($(".mobile"), $(".mobile_box .mobile_err"));
        checkDesc($(".desc textarea"), $(".desc_box .desc_err"));
        
        if (account.realName &&
            account.email &&
            account.mobile &&
            account.desc) {
            
            //todo 提交代码
            
        }
    });
        
        //添加经历
    var experience = {
        commpanyName: false,
        job_title: false
    };
    
        //创业经历
    function checkEnterpriserExperience(elm) {
        var val = elm.val();
        
        if (empty(val)) {
            sendErrorMessage($(".enter .add_comm_err"), "公司简称不能为空！");
            experience.commpanyName = false;
            return;
        }
        
        if (val.length > 30) {
            sendErrorMessage($(".enter .add_comm_err"), "公司简称长度不能超过30个字符！");
            experience.commpanyName = false;
            return;
        }
        experience.commpanyName = true;
    }
    
    doc.on("blur", ".enter_f .comm input", function() {
        checkEnterpriserExperience($(this));
    });
    doc.on("focus", ".enter_f .comm input", function() {
        $(".add_comm_err").html("").hide();
    });
    
        //公司职位
    function checkJobTitle(elm) {
        var val = elm.val();
        
        if (empty(val)) {
            sendErrorMessage($(".enter .add_job_err"), "公司职位不能为空！");
            experience.job_title = false;
            return;
        }
        
        if (val.length > 10) {
            sendErrorMessage($(".enter .add_job_err"), "公司职位长度不能超过10个字符！");
            experience.job_title = false;
            return;
        }
        experience.job_title = true;
    }
    
    doc.on("blur", ".enter_f .job input", function() {
        checkJobTitle($(this));
    });
    
    doc.on("focus", ".enter_f .job input", function() {
        $(".add_job_err").html("").hide();
    });
    
    doc.on("click", ".enter .checkElm [type=checkbox]", function() {
        if ($(this).prop("checked") === true) {
            $(".enter .end-time").show();
        } else {
            $(".enter .end-time").hide();
        }
    });
    
        //工作经历
    var worked = {
        commpanyName: false,
        job_title: false
    };
    
    function checkCommName(elm, err, msg, msg2) {
        var val = elm.val();
        
        if (empty(val)) {
            sendErrorMessage(err, msg);
            worked.commpanyName = false;
            return;
        }
        
        if (val.length > 30) {
            sendErrorMessage(err, msg2);
            worked.commpanyName = false;
            return;
        }
        worked.commpanyName = true;
    }
    
    function checkJobName(elm, err, msg, msg2) {
        var val = elm.val();
        
        if (empty(val)) {
            sendErrorMessage(err, msg);
            worked.job_title = false;
            return;
        }
        
        if (val.length > 10) {
            sendErrorMessage(err, msg2);
            worked.job_title = false;
            return;
        }
        worked.job_title = true;
    }
    
    doc.on("blur", ".work_f .comm input", function() {
        checkCommName($(this), $(".work_comm_err"), "公司简称不能为空！", "公司简称长度不能超过30个字符！");
    });
    
    doc.on("focus", ".work_f .comm input", function() {
        $(".work_comm_err").html("").hide();
    });
    
    doc.on("blur", ".work_f .job input", function() {
        checkJobName($(this), $(".work_job_err"), "公司职位不能为空！", "公司职位长度不能超过10个字符！");
    });
    
    doc.on("focus", ".work_f .job input", function() {
        $(".work_job_err").html("").hide();
    });
    
    doc.on("click", ".work_f .checkElm [type=checkbox]", function() {
        if ($(this).prop("checked") === true) {
            $(".work_f .end-time").show();
        } else {
            $(".work_f .end-time").hide();
        }
    });
        //插入节点 (创业经历)
    function innerEnteHtml(elm, name, job_title) {
        elm.before('<div class="dl-box">' +
                        '<dl>' +
                            '<dt><a href="#"><img src="images/userphoto_5.png"></a></dt>' +
                            '<dd>' +
                                '<p><strong>' + name + '</strong></p>' +
                                '<p><em>' + job_title + '</em></p>' +
                                '<time>2014/12 - 至今</time>' +
                            '</dd>' +
                        '</dl>' +
                    '</div>');
    }
    
    doc.on("click", ".enter_f .create_btn .add", function() {
        checkEnterpriserExperience($(".add_create .comm input"));
        checkJobTitle($(".add_create .job input"));
        
        if (experience.commpanyName &&
            experience.job_title) {
            innerEnteHtml($(".enter .addWork"), $(".enter .comm input").val(),
                          $(".enter .job input").val());
        }
    });
    
        //插入节点 (工作经历)
    doc.on("click", ".work_f .add", function() {        
        checkCommName($(".work_f .comm input"), $(".work_comm_err"), "公司简称不能为空！", "公司简称长度不能超过30个字符！");
        checkJobName($(".work_f .job input"), $(".work_job_err"), "公司职位不能为空！", "公司职位长度不能超过10个字符！");
        
        if (worked.commpanyName &&
            worked.job_title) {
                innerEnteHtml($(".content-box .addWork"), $(".work_f .comm input").val(),
                          $(".work_f .job input").val());
        }
    });
    
    
    /***账号信息end***/
    
    
    /***投资管理 -start-***/
    var money = {
        min_v_1: false,
        max_v_2: false,
        min_v_3: false,
        max_v_4: false,
        compare_1: false,
        compare_2: false
    },
    
    //错误信息(最小值)
    errorMeg = [
        "最低金额不能为空！",
        "最低金额必须为数字！",
        "最低金额不能小于1！"
    ],
    //错误信息(最高值)
    errorMeg_max = [
        "最高金额不能为空！",
        "最高金额必须为数字！",
        "最高金额不能小于1！"
    ];
    
    function isNumber(num) {
        var reg = /^\d+$/;
        return reg.test(num);
    }
    
    function checkMoney(val, obj, key, node, error) {
        
        if (empty(val)) {
            sendErrorMessage(node, error[0]);
            obj[key] = false;
            return;
        }
        
        if (!isNumber(val)) {
            sendErrorMessage(node, error[1]);
            obj[key] = false;
            return;
        }
        
        if (val < 1) {
            sendErrorMessage(node, error[2]);
            obj[key] = false;
            return;
        }
        
        obj[key] = true;
    }
    
        //个人投资
        //最小值
    doc.on("blur", ".min_v_1 input", function() {
        var val = $(this).val();
        checkMoney(val, money, "min_v_1", $(".v_1_err"), errorMeg); //参数为 表单值，money对象，错误信息节点, 错误信息
        
    });
    doc.on("focus", ".min_v_1 input", function() {
        $(".v_1_err").html("").hide();
        $(".v_2_err").html("").hide();
    });
    
        //最高值
    doc.on("blur", ".max_v_2 input", function() {
        var val = $(this).val();
        checkMoney(val, money, "max_v_2", $(".v_2_err"), errorMeg_max); //参数为 表单值，money对象，错误信息节点, 错误信息
    });
    
    doc.on("focus", ".max_v_2 input", function() {
        $(".v_1_err").html("").hide();
        $(".v_2_err").html("").hide();
    });
    
        //机构
        //最小值
    doc.on("blur", ".min_v_3 input", function() {
        var val = $(this).val();
        checkMoney(val, money, "min_v_3", $(".v_3_err"), errorMeg); //参数为 表单值，money对象，错误信息节点, 错误信息
    });
    doc.on("focus", ".min_v_3 input", function() {
        $(".v_3_err").html("").hide();
        $(".v_4_err").html("").hide();
    });
    
        //最高值
    doc.on("blur", ".max_v_4 input", function() {
        var val = $(this).val();
        checkMoney(val, money, "max_v_4", $(".v_4_err"), errorMeg_max); //参数为 表单值，money对象，错误信息节点, 错误信息
    });
    doc.on("focus", ".max_v_4 input", function() {
        $(".v_3_err").html("").hide();
        $(".v_4_err").html("").hide();
    });
    
        //提交表单
        //检查投资信息最低值是否大于最高值
    function compare(min, max, err, obj, key) {
        //if (money[key] && money[key]) {
            if (min > max) {
                money[key] = false;
                sendErrorMessage(err, "最低金额不能高于最高金额！");
                return;
            }
        //}
        obj[key] = true;
    }
    
    doc.on("click", ".submit_btn button", function() {
        var minV1 = $(".min_v_1 input").val(),
            maxV2 = $(".max_v_2 input").val(),
            minV3 = $(".min_v_3 input").val(),
            maxV4 = $(".max_v_4 input").val();
            
        checkMoney(minV1, money, "min_v_1", $(".v_1_err"), errorMeg);
        checkMoney(maxV2, money, "max_v_2", $(".v_2_err"), errorMeg_max);
        checkMoney(minV3, money, "min_v_3", $(".v_3_err"), errorMeg);
        checkMoney(maxV4, money, "max_v_4", $(".v_4_err"), errorMeg_max);
        
        compare(minV1, maxV2, $(".v_5_err"), money, "compare_1");   //对比最低金额和最高金额
        compare(minV3, maxV4, $(".v_6_err"), "compare_2");  //对比最低金额和最高金额
        
        if (money.min_v_1 &&
            money.max_v_2 &&
            money.min_v_3 &&
            money.max_v_4) {
            
            if (money.compare_1 &&
                money.compare_2) {
                //todo提交表单
                
            }
        }
    });
    
        //添加投资经历
    var inves = {
        projectName: false,
        finanAmount: false
    };
        //项目名称
    function checkProName(elm) {
        var val = elm.val();

        if (val.length > 30) {
            sendErrorMessage($(".pro_name_error"), "项目名称长度不能大于30个字符！");
        }
    }

    doc.on("blur", ".pro-name-inv input", function() {
        checkProName($(this));
    });
    doc.on("focus", ".pro-name-inv input", function() {
        $("pro_name_error").html("").hide();
    });
        
        //融资额度
    function finanAmount(elm) {
        var val = elm.val();
        
        if (empty(val)) {
            sendErrorMessage($(".inv_grade_error"), "融资额度不能为空！");
            inves.finanAmount = false;
            return;
        }
        
        if (!isNumber(val)) {
            sendErrorMessage($(".inv_grade_error"), "融资额度必须为数字！");
            inves.finanAmount = false;
            return;
        }
        inves.finanAmount = true;
    }

    doc.on("blur", ".inv-grade input", function() {
        finanAmount($(this));
    });
    doc.on("focus", ".inv-grade input", function() {
        $(".inv_grade_error").html("").hide();
    });
    
        //投资主体
    doc.on("blur", ".inv-body input", function() {
        var val = $(this).val();
        
        if (empty(val)) {
            return;
        }
        
        if (!isNumber(val)) {
            sendErrorMessage($(".inv_body_error"), "融资额度必须为数字！");
            return;
        }
    });
    
        //插入内容
    function addInvestment(elm, proName, stage, money, finan, date) {
        elm.append(
            '<div class="dl-box">' +
                '<dl>' +
                '<dt><a href="#"><img src="images/userphoto_5.png"></a></dt>' +
                '<dd>' +
                    '<p><strong>' + proName + '</strong></p>' +
                    '<p><em>' + stage + '</em>红杉资本500万美元</p>' + 
                    '<time>' + date + '</time>' +
                '</dd>' +
                '</dl>' +
            '</div>'
        );
    }
    
    doc.on("click", ".add-inv .add-btn", function() {
        addInvestment($(".table-box"),
                      $(".pro-name-inv input").val(), //项目名称
                      $(".inv-count select").val(), //融资轮资
                      $(".inv-grade select").val(), $(".inv-grade input").val(), //融资，融资额度
                      $(".inv-date .year").val() + "/" + $(".inv-date .month").val()); //融资时间
    });
    /***投资管理 -end-***/
    
    
    /***融资平台 -start-***/
    var finanInfo = {
        commpanyName: false,
        minMoney: false,
        maxMoney: false,
        desc: false,
        compare: false
    };
    
        //修改信息
    function checkCommpany(elm) {
        var val = elm.val();
        
        if (empty(val)) {
            sendErrorMessage($(".comm_error"), "企业名称不能为空！");
            finanInfo.commpanyName = false;
            return;
        }
        
        if (val.length > 30)  {
            sendErrorMessage($(".comm_error"), "企业名称长度不能超过30个字符！");
            finanInfo.commpanyName = false;
            return;
        }
        
        finanInfo.commpanyName = true;
    }
    
    doc.on("blur", ".comm-name input", function() {
        checkCommpany($(this));
    });
    doc.on("focus", ".comm-name input", function() {
        $(".comm_error").html("").hide();
    });
    
        //投资金额
        //最小金额
    doc.on("blur", ".minMoney", function() {
        var val = $(this).val();
        checkMoney(val, finanInfo, "minMoney", $(".minMoney_err"), errorMeg);
    });
    doc.on("focus", ".minMoney", function() {
        $(".minMoney_err").html("").hide();
        $(".maxMoney_err").html("").hide();
    });
    
        //最小金额
    doc.on("blur", ".maxMoney", function() {
        var val = $(this).val();
        checkMoney(val, finanInfo, "maxMoney", $(".maxMoney_err"), errorMeg_max);
    });
    
    doc.on("focus", ".maxMoney", function() {
        $(".maxMoney_err").html("").hide();
        $(".minMoney_err").html("").hide();
    });
    
        //企业介绍
    function checkBusinessDesc(val) {
        if (empty(val)) {
            sendErrorMessage($(".comm_desc_err"), "企业介绍不能为空！");
            finanInfo.desc = false;
            return;
        }
        
        if (val.length > 500) {
            sendErrorMessage($(".comm_desc_err"), "企业介绍长度不能超过500个字符！");
            finanInfo.desc = false;
            return;
        }
        finanInfo.desc = true;
    }
    
    doc.on("blur", ".terr-box .desc textarea", function() {
        var val = $(this).val();
        checkBusinessDesc(val);
    });

    doc.on("focus", ".terr-box .desc textarea", function() {
        $(".terr-box .comm_desc_err").html("").hide();
    });
    
        //提供服务
    function innerList(ul) {
        var val = $(".service input").val(); 
        
        if (!empty(val)) {
            ul.append('<li>' + val + '<em class="close">x</em></li>');
        }
    }
    
    function removeElement(node) {
        node.addClass("fadeout");
        clearInterval(node.timer);
        node.timer = setTimeout(function() {
            node.remove();
        }, 500);
    }
    
    doc.on("click", ".service button", function() {
        innerList($(".service .tag ul"));
    });
    
    doc.on("click", ".tag .close", function() {
        var btn = $(this),
            parent = btn.parent();
        removeElement(parent);
    })
    
        //项目条件(未完成)
    var count = {
        num: 1
    };
    
    function innerInput(elm) {
        elm.append('<p><button class="remove_btn">X</button><input type="text" value="' + count.num + '. " /></p>');
        count.num++;
    }
    
    doc.on("click", ".pro-cond .pro-cond-btn", function() {
        innerInput($(".pro-cond > div"));
    });
    
    doc.on("click", ".pro-cond .remove_btn", function() {
        var parent = $(this).parent();
        removeElement(parent);
    });
    
        //成功案例
    function innerDL(elm, box) {
        var val = elm.val();
        
        
        if (!empty(val)) {
            box.append('<dl>' +
                    '<dt><a href="#"><img src="images/userphoto_5.png"></a></dt>' +
                    '<dd>' + val + '</dd>' +
                    '<dl>');
        }
        
        elm.val("");
    }
    
    doc.on("click", ".case button", function() {
        innerDL($(".case input"), $(".case div"));
    });
    
        //提交表单
    doc.on("click", ".submit_btn", function() { 
        checkCommpany($(".comm-name input"));
        checkBusinessDesc($(".terr-box .desc textarea"));
        checkMoney($(".minMoney").val(), finanInfo, "minMoney", $(".minMoney_err"), errorMeg);
        checkMoney($(".maxMoney").val(), finanInfo, "maxMoney", $(".maxMoney_err"), errorMeg_max);
        
        compare($(".minMoney").val(), $(".maxMoney").val(), $(".compare_err"), finanInfo, "compare");
        
        //全部验证
        if (finanInfo.commpanyName &&
            finanInfo.minMoney &&
            finanInfo.maxMoney &&
            finanInfo.desc &&
            finanInfo.compare) {
            
            //TODU 提交个人信息
            
        }
        
    });
    /***融资平台 -end-***/
    
    
    /***交互***/
    var M = {};
    
    M.PosElement = function() {
        var that = this;
    };
    
    pe = M.posElement = new M.PosElement();
    
    M.posElm = function(event) {
        
        var doc     = document,
            name    = doc.querySelector(".message .name"),
            target  = event.target,
            classname = target.className,
            top     = target.offsetTop,
            left    = target.offsetLeft,
            box     = doc.querySelector(".focus-box"),
            user    = box.querySelector("dd p"),
            mirror  = doc.querySelector(".mirror");
            
        if ( classname.indexOf("name") > -1 && classname.indexOf("pro-name") !== 0 ) {
            user.innerHTML = target.innerHTML;
            box.style.top = top + 30 + "px";
            box.style.left = left + "px";
            box.style.display = "block";
            mirror.style.display = "block";
        }
    };
    
    doc_.querySelector(".message").addEventListener("click", function(event) {
        M.posElm(event);
    }, false);
    
    doc_.querySelector(".mirror").addEventListener("click", function(event) {
        doc_.querySelector(".focus-box").style.display = "none";
    }, false);
    
    //640px滚动导航栏
    M.Scroll = function() {
        var that = this;
        that.header = doc_.querySelector("header");
        that.fixedBar = doc_.querySelector(".mob-fixed-bar");
        that.height = that.header.offsetHeight;
        
    };
    scroll = M.scroll = new M.Scroll();
    
    scroll.reSize = function() {
        if (utils.offset().width < 640 && utils.scroll().top > scroll.fixedBar.offsetTop) {
            // console.log(scroll.fixedBar.className.length);
            if (scroll.fixedBar.className.length <= 13) {
                scroll.fixedBar.className += " fixed-bar";
            }
            
        } else {
            scroll.fixedBar.className = "mob-fixed-bar";
        }
    };
    scroll.reSize();
    
    window.addEventListener("scroll", function() {
        var scroll = utils.scroll(),
            left = scroll.left,
            top = scroll.top;
    }, false);

    window.addEventListener("resize", function() {
        scroll.reSize();
    }, false);
    
    doc_.addEventListener("scroll", function() {
        scroll.reSize();
    }, false);
});